package main
 
import (
    "fmt"
    "sort"
)
 
type Data struct {
    Key        string
    value      string
    OperaTime      string
}
 
type DataSlice []Data
 
func (s DataSlice) Len() int  { return len(s) }
func (s DataSlice) Swap(i, j int)     { s[i], s[j] = s[j], s[i] }
 
type ByOperaTime struct{ DataSlice }    // 将 PersonSlice 包装起来到 ByName 中
 
func (s ByOperaTime) Less(i, j int) bool     { return s.DataSlice[i].OperaTime < s.DataSlice[j].OperaTime }    // 将 Less 绑定到 ByName 上
 

func main() {
    s := []Data{
        {"apple", "基础数据","2022-01-01 10:11:12"},
        {"zhihuan", "验证秒","2022-01-01 10:11:13"},
        {"world", "验证分","2022-01-01 10:12:12"},
        {"hello", "验证时","2022-01-01 11:11:12"},
        {"orange", "验证日","2022-01-02 10:11:12"},
        {"pear", "验证年","2023-01-01 10:11:12"},
        {"banana", "验证月","2022-02-01 10:11:12"},
    }
 
    sort.Sort(ByOperaTime{s})
    fmt.Println("\nData by OperaTime:")
    printPeople(s)
 
}
 
func printPeople(s []Data) {
    for _, o := range s {
        fmt.Printf("%-8s (%s) [%s]\n", o.Key, o.value,o.OperaTime)
    }
}